package com.frontop.bigdata.modules.controller;

import com.frontop.common.util.JwtUtil;
import com.frontop.common.util.ResultJsonUtil;
import com.frontop.dubbo.api.auth.Oauth2Api;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author YangBoos
 * @title: TUserController
 * @projectName frontop-cloud
 * @description: TODO
 * @date 2022/2/22 11:35
 */
@RestController
@RequestMapping("/user")
public class TUserController {
    @Reference
    private Oauth2Api oauth2Api;

    @ApiOperation(value = "用户个人信息")
    @GetMapping("/selectShowUserInfo")
    public ResultJsonUtil<Object> selectShowUserInfo(Authentication authentication) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        return new ResultJsonUtil().success(oauth2Api.selectShowUserInfo((String) claims.get("user_name")));
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/upPwd")
    public ResultJsonUtil<Object> upPwd(Authentication authentication,String password) {
        Claims claims = JwtUtil.getJwtInfo(authentication);
        if(StringUtils.isEmpty(password)){
            return new ResultJsonUtil().customFail("密码不能为空");
        }
        boolean isSuss = oauth2Api.upPwd((String) claims.get("user_name"),password);
        if(isSuss){
            return new ResultJsonUtil().success("密码修改成功");
        }else {
            return new ResultJsonUtil().customFail("请求失败");
        }
    }

}
